
from paddleocr import PaddleOCR
from PIL import Image, ImageGrab
im = ImageGrab.grabclipboard()
if isinstance(im, Image.Image):
	with open("pic.png", "wb") as f:
		im.save(f)
# ocr = PaddleOCR(use_angle_cls=True, lang='ch')
ocr = PaddleOCR(use_angle_cls=True, lang='en')
data = ocr.ocr('pic.png',cls=True)

def matrixocr():
	global data
	elements = ''
	boundbox =  data[0][0][0]
	lastY = boundbox[0][1]
	fontsize = boundbox[2][1] - boundbox[0][1]
	excluded = set()
	excluded.add("=")
	excluded.add("[")
	excluded.add("]")
	toExc = input("any char to exclude?")
	for e in toExc:
		excluded.add(e)
	for i in range(len(data)):
		for j in range(len(data[i])):
			element = data[i][j]
			fig = element[1][0]
			for e in excluded:
				fig = fig.replace(e, "")
			if len(fig) == 0:
				break
			y = element[0][0][1]
			if abs(y-lastY) > fontsize*1.2:
				elements += '\n'
				lastY = y
			# elements += str(fig) + "\t"
			elements += fig + "\t"
			elements = elements.replace("\t\t", "\t")
	print(elements)

	with open('ocr.txt', 'wt') as f:
		f.write(elements)

def normalocr():
	global data
	elements = ''
	boundbox =  data[0][0][0]
	lastY = boundbox[0][1]
	fontsize = boundbox[2][1] - boundbox[0][1]
	for i in range(len(data)):
		for j in range(len(data[i])):
			element = data[i][j]
			fig = element[1][0]
			y = element[0][0][1]
			if abs(y-lastY) > fontsize*1.2:
				elements += '\n'
				lastY = y
			elements += str(fig) + "\t"
	elements += '\n'
	with open('normal.txt', 'at', encoding='utf-8') as f:
		f.write(elements)
	
matrixocr()


